﻿using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sidvall.IdentityManager.UI.ViewModels
{
    public class NewsViewModel : Sidvall.UI.ViewModels.ViewModelBase
    {
        #region Public Members

        #region Created

        private string _Created;
        public string Created
        {
            get
            {
                return _Created;
            }
            set
            {
                _Created = value;
                RaisePropertyChanged("Created");
            }
        }

        #endregion
        #region FileVersion

        private string _FileVersion;
        public string FileVersion
        {
            get
            {
                return _FileVersion;
            }
            set
            {
                _FileVersion = value;
                RaisePropertyChanged("FileVersion");
            }
        }

        #endregion
        #region NewsHtml

        private string _NewsHtml;
        public string NewsHtml
        {
            get
            {
                return _NewsHtml;
            }
            set
            {
                _NewsHtml = value;
                RaisePropertyChanged("NewsHtml");
            }
        }

        #endregion

        #region StartAsync

        protected override async Task StartAsync()
        {
            List<Task> tasks;

            tasks = GetLoadTasks();
            await Task.WhenAll(tasks);
            this.NewsHtml = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\News.htm");
        }

        #endregion

        #endregion
        #region Private Members

        // Tasks
        #region GetLoadTasks

        private List<Task> GetLoadTasks()
        {
            List<Task> tasks;

            tasks = new List<Task>();
            tasks.Add(LoadAssemblyTimestampAsync());
            tasks.Add(LoadAssemblyVersionAsync());
            return tasks;
        }

        #endregion
        #region LoadAssemblyTimestampAsync

        private async Task LoadAssemblyTimestampAsync()
        {
            var time = await Sidvall.SystemContext.Current.PackageManager.GetInstalledDateAsync();
            this.Created = time.ToString();
        }

        #endregion
        #region LoadAssemblyVersionAsync

        private async Task LoadAssemblyVersionAsync()
        {
            var version = await Sidvall.SystemContext.Current.PackageManager.GetVersionAsync();
            this.FileVersion = version.ToString();
        }

        #endregion

        #endregion
    }
}
